package com.zz.spring6.validator.four;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;


public class CannotBlankValidation implements ConstraintValidator<CannotBlank,String> {
    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (value !=null && value.contains(" ")){
            // 获取默认提醒信息
            String defaultConstraintMessageTemplate = context.getDefaultConstraintMessageTemplate();
            System.out.println("default message" + defaultConstraintMessageTemplate);
            // 禁用默认提示信息
            context.disableDefaultConstraintViolation();
            // 设置提示信息
            context.buildConstraintViolationWithTemplate("can not" +
                    "contains blank").addConstraintViolation();
            return false;
        }
        return true;
    }
}
